package com.likou;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class q46 {

    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> ans = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        boolean[] isUsed = new boolean[nums.length];

        DFS(nums, 0, ans, list, isUsed);

        return ans;
    }

    private void DFS(int[] nums, int index, List<List<Integer>> ans, List<Integer> list, boolean[] isUsed) {
        if (index == nums.length) {
            ans.add(new ArrayList<>(list));
            return;
        }

        for (int i = 0; i < nums.length; i++) {
            if (!isUsed[i]) {
                list.add(nums[i]);
                isUsed[i] = true;
                DFS(nums, index + 1, ans, list, isUsed);
                isUsed[i] = false;
                list.remove(list.size() - 1);
            }
        }

    }
}
